// pages/mine/login/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    label:1,
    disbutton:false,
    eyeclose:1,
    eyeUrl:"/asset/img/eye-hide.png",
    eye_open:"/asset/img/eye-show.png",
    eye_close:"/asset/img/eye-hide.png",
    eye_type:"password",
    getcodemess:"获取验证码",
    disbtn:false,
    number:"",
    numhide:"",
    codehide:"hide"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 栏目切换事件  
   */
  checkedLabel:function(e){
    var that = this;
    var type = e.currentTarget.id;
    if(type != that.data.label){
      that.setData({
        label: type
      })
      if (type == 1) {
        that.setData({
          numhide: "",
          codehide: "hide"
        })
      } else {
        that.setData({
          numhide: "hide",
          codehide: ""
        })
      }
    }
  },

  /**
   * 密码显示点击事件 
   */
  eye:function(e){
    var that = this;
    let id = e.currentTarget.id;
    if(id == 1){
      that.setData({
        eyeclose:0,
        eyeUrl:that.data.eye_open,
        eye_type:"text"
      })
    }else{
      that.setData({
        eyeclose: 1,
        eyeUrl: that.data.eye_close,
        eye_type:"password"
      })
    }
  },

  /**手机号码赋值操作**/
  setMobile:function(e){
    this.setData({
      number:e.detail.value
    })
  },

  /**获取验证码**/
  getCode:function(){
    var that = this;
    that.setData({
      disbtn:true
    })
    var number = that.data.number;
    let v = /^1[3456789]\d{9}$/;
    if ( ! number && ! v.test(number) ) {
      wx.showToast({
        title: '手机号码格式错误',
        icon: 'none',
        duration: 1500
      });
      that.setData({
        disbtn:false
      })
      return false;
    }
    // wx.request({
    //   url: serverUrl+'api/v1/getCode',
    //   method:"GET",
    //   data:{mobile:number},
    //   dataType:"json",
    //   header: {
    //     //设置参数内容类型为x-www-form-urlencoded
    //     'content-type': 'application/x-www-form-urlencoded',
    //     'Accept': 'application/json'
    //   },
    //   success:function(e){
    //     if(e.data.code == 200){
    //       var i = 60;
    //       var timer = setInterval(function () {
    //         if (i <= 60 && i > 0) {
    //           i--;
    //           that.setData({
    //             getcodemess: i + "秒"
    //           })
    //         } else {
    //           i = 60;
    //           that.setData({
    //             getcodemess: "获取验证码",
    //             disbtn: false
    //           })
    //           clearInterval(timer);
    //         }
    //       }, 1000)
    //     } else {
    //       wx.showToast({
    //         title: e.data.message,
    //         icon: 'none',
    //         duration: 1500
    //       });
    //       that.setData({
    //         disbtn: false
    //       })
    //     }
    //   },
    //   error:function(e){
    //     wx.showToast({
    //       title: "服务器错误",
    //       icon: 'none',
    //       duration: 1500
    //     });
    //     that.setData({
    //       disbtn: false
    //     })
    //   }
    // })
  },

  /**小程序登录**/
  onGotUserInfo:function(e){
    var that = this;
    var uid = wx.getStorageSync('uid');
    if ( ! uid ) {
      util.login(serverUrl + "api/v1/users/authories2");
      wx.showToast({
        title: '已授权!请您再次登录',
        icon: "none",
        duration: 2000
      })
    }
    if (e.detail.errMsg != 'getUserInfo:ok'){
      wx.showToast({
        title: '您已取消微信登录',
        icon: "none",
        duration: 1500
      })
      return false;
    }
    var detail = e.detail.userInfo;
    var sex = 0;
    if(detail.gender){
      sex = detail.gender-1;
    }
    var rdata = {
      uid: uid,
      nickname: detail.nickName,
      image: detail.avatarUrl,
      sex: sex
    };
    let location = wx.getStorageSync('location');
    if(location){
      rdata.lat = location.lat;
      rdata.lon = location.lon;
    }
    wx.request({
      url: serverUrl+'/api/v1/users/addinfos',
      method: "POST",
      dataType: "json",
      data:rdata,
      success:function(data){
        data = data.data;
        if(data.code == 200){
          let userInfo = {};
          userInfo.nickname = data.data.nickname;
          userInfo.area = data.data.area;
          userInfo.city = data.data.city;
          userInfo.prov = data.data.prov;
          userInfo.email = data.data.email;
          userInfo.headimg = data.data.headimg;
          userInfo.uid = data.data.id;
          userInfo.lat = data.data.lat;
          userInfo.lon = data.data.lon;
          userInfo.member_type = data.data.member_type;
          userInfo.mobile = data.data.mobile;
          userInfo.number = data.data.number;
          userInfo.sex = data.data.sex;
          userInfo.is_auth = data.data.is_auth;
          wx.setStorageSync("userInfo", userInfo);
          wx.switchTab({
            url: '../index/index',
          })
        } else {
          console.log(data)
          wx.showToast({
            title: '登录失败:【'+data.message+'】',
            icon: "none",
            duration: 2000
          })
        }
      },
      error:function(data){
        wx.showToast({
          title: '服务器错误!登录失败',
          icon: "none",
          duration: 1500
        })
      }
    })
  },

  /**密码登录**/
  pwdLogin:function(e){
    let number = e.detail.value.number;
    let password = e.detail.value.password;
    let code = e.detail.value.code;
    let mv = /^1[3456789]\d{9}$/;
    let ev = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
    let nv = /^\d{8}$/;
    let pv = /^[_a-zA-Z0-9]{6,18}$/;
    if ( !number || ( !mv.test(number) && !ev.test(number) && !nv.test(number) ) ) {
      wx.showToast({
        title: '账号格式错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if ( (!password || !pv.test(password) && ( !code || !/^\d{4}$/.test(code) ) ) ) {
      wx.showToast({
        title: '密码或验证码格式错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var data = {};
    data.number = number;
    if ( password ) {
      data.password = password;
    } else {
      data.code = code;
    }
    let location = wx.getStorageSync('location');
    if (location) {
      data.lat = location.lat;
      data.lon = location.lon;
    }
    wx.request({
      url: serverUrl+'api/v1/users/logins',
      method:"POST",
      dataType:"json",
      data:data,
      success:function(data){
        data = data.data;
        if (data.code == 200) {
          let userInfo = {};
          userInfo.nickname = data.data.nickname;
          userInfo.area = data.data.area;
          userInfo.city = data.data.city;
          userInfo.prov = data.data.prov;
          userInfo.email = data.data.email;
          userInfo.headimg = data.data.headimg;
          userInfo.uid = data.data.id;
          userInfo.lat = data.data.lat;
          userInfo.lon = data.data.lon;
          userInfo.member_type = data.data.member_type;
          userInfo.mobile = data.data.mobile;
          userInfo.number = data.data.number;
          userInfo.sex = data.data.sex;
          userInfo.is_auth = data.data.is_auth;
          wx.setStorageSync("userInfo", userInfo);
          wx.switchTab({
            url: '../index/index',
          })
        } else {
          wx.showToast({
            title: '登录失败:【' + data.message + '】',
            icon: "none",
            duration: 2000
          })
        }
      },
      error:function(data){
        wx.showToast({
          title: '服务器错误!登录失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**注册页面跳转**/
  jump:function(){
    wx.navigateTo({
      url: '../register/index',
    })
  },

  /**首页跳转**/
  returnIndex:function(){
    wx.switchTab({
      url: '../../index/index',
    })
  }
})